package gui;

import controller.LoginController;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class LoginView extends JPanel{

    private JTextField hostFld = new JTextField();
    private JTextField portFld = new JTextField(5);
    private JTextField usernameFld = new JTextField();
    private JPasswordField passwordFld = new JPasswordField();
    
    public LoginView() {
        GridLayout layout = new GridLayout(5,1);
        setLayout(layout);
        add(buildHorizontalPanel("Host", hostFld));
        add(buildHorizontalPanel("Porta", portFld));
        add(buildHorizontalPanel("Username", usernameFld));
        add(buildHorizontalPanel("Password", passwordFld));
        add(buildConnectButton());
        
        hostFld.setText("localhost");
        portFld.setText("3306");
        usernameFld.setText("root");
    }

    private JPanel buildHorizontalPanel(String label, Component cmp) {
        JPanel res = new JPanel();
        GridLayout layout = new GridLayout(1, 2);
        res.setLayout(layout);
        res.add(new JLabel(label));
        res.add(cmp);
        return res;
    }
    
    private JButton buildConnectButton(){
        JButton connectButton = new JButton("Connetti");
        connectButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                LoginController.getInstance().connect();
            }
        });
        return connectButton;
    }

    public String getHost() {
        return hostFld.getText();
    }

    public String getPort() {
        return portFld.getText();
    }
    
    public String getUsername(){
        return usernameFld.getText();
    }
    
    public String getPassword(){
        return new String(passwordFld.getPassword());
    }
}
